/**
 * Created by GadflyBSD on 16/3/12.
 */
myApp.directive('myWidget', ['$filter', function($filter){
	return {
		restrict: 'EA',
		template: function(elem, attr){
			switch(attr.myWidget){
				case 'button':
					return '<button class="btn btn-success btn-xs" ng-click="show()"><i class="fa fa-eye"></i> 查看</button> &nbsp' +
					       '<button class="btn btn-danger btn-xs" ng-click="refusal()"><i class="fa fa-edit"></i> 编辑</button>' +
					       '<button class="btn btn-info btn-xs" ng-click="refusal()"><i class="fa fa-edit"></i> 签约</button>';
				case 'status':
					return '<span class="label label-warning">尚未签约</span>' +
					       '<span class="label label-danger">签约已过期</span>' +
					       '<span class="label label-info"></span>';
				case 'setting':
					return '<button class="btn btn-danger btn-xs" ng-click="setting()"><i class="fa fa-gear"></i> 设定</button>';
				case 'checkWithdraw':
					return '<button class="btn btn-xs btn-info btn-block" ng-click="checkWithdraw()">审核</button>';
				case 'checkWithdrawAll':
					return '<button class="btn btn-sm btn-warning btn-block" ng-click="checkWithdrawAll()">批量审核</button>';
			}
		},
		scope: {sid: '@forSid', vip: '@forVip', key: '@forKey', title: '@forTitle'},
		transclude: true,
		controller: function ($scope, $element, $attrs, shopService, $uibModal, dialogs) {
			var data = {
				ctitle: $scope.title,
				ckey: $scope.key,
			}
			angular.extend($scope, {
				show: function(){
					shopService.getShopInfo($scope.sid).success(function(resp){
						var modalInstance = $uibModal.open({
							animation: true,
							templateUrl: 'showModalContent.html',
							controller: function($scope, $uibModalInstance, items) {
								$scope.shop = items;
								$scope.cancel = function() {
									$uibModalInstance.dismiss('cancel');
								};
							},
							resolve: {
								items: function(){
									return resp.data;
								}
							}
						});
						modalInstance.result.then(function(selectedItem) {
							$scope.selected = selectedItem;
						}, function(){});
					});
				},
				refusal: function(){
					shopService.getShopInfo($scope.sid).success(function(resp){
						var modalInstance = $uibModal.open({
							animation: true,
							templateUrl: 'refusalModalContent.html',
							controller: function($scope, $uibModalInstance, items) {
								$scope.shop = items;
								$scope.submit = function(){
									$scope.shop.vip_limit = Date.parse($scope.shop.dt)/1000;
									shopService.setVipLimit($scope.shop).success(function(resp){
										dialogs.error(resp.title, resp.content).result.then(function(){
											location.reload(true);
										});
									});
									$uibModalInstance.close();
								};
								$scope.cancel = function() { $uibModalInstance.dismiss('cancel'); };
								$scope.today = function() {
									$scope.shop.dt = new Date();
								};
								$scope.today();
								$scope.clear = function() {
									$scope.shop.dt = null;
								};
								$scope.inlineOptions = {
									customClass: getDayClass,
									minDate: new Date(),
									showWeeks: true
								};
								$scope.dateOptions = {
									dateDisabled: disabled,
									formatYear: 'yy',
									maxDate: new Date(2020, 5, 22),
									minDate: new Date(),
									startingDay: 1
								};
								function disabled(data) {
									var date = data.date,
										mode = data.mode;
									return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
								}
								$scope.toggleMin = function() {
									$scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null : new Date();
									$scope.dateOptions.minDate = $scope.inlineOptions.minDate;
								};
								$scope.toggleMin();
								$scope.calendar = function() {
									$scope.popup.opened = true;
								};
								$scope.setDate = function(year, month, day) {
									$scope.shop.dt = new DateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(year, month, day));
								};
								$scope.formats = ['yyyy年MM月dd日', 'yyyy/MM/dd', 'yyyy-MM-dd', 'shortDate'];
								$scope.format = $scope.formats[0];
								$scope.altInputFormats = ['yyyy年MM月dd日'];
								$scope.popup = {opened: false};
								var tomorrow = new Date();
								tomorrow.setDate(tomorrow.getDate() + 1);
								var afterTomorrow = new Date();
								afterTomorrow.setDate(tomorrow.getDate() + 1);
								$scope.events = [
									{date: tomorrow, status: 'full'},
									{date: afterTomorrow, status: 'partially'}
								];
								function getDayClass(data) {
									var date = data.date,
										mode = data.mode;
									if (mode === 'day') {
										var dayToCheck = new Date(date).setHours(0,0,0,0);
										for (var i = 0; i < $scope.events.length; i++) {
											var currentDay = new Date($scope.events[i].date).setHours(0,0,0,0);
											if (dayToCheck === currentDay) {
												return $scope.events[i].status;
											}
										}
									}
									return '';
								}
							},
							resolve: {
								items: function(){
									return resp.data;
								}
							}
						});
						modalInstance.result.then(function(selectedItem) {
							$scope.selected = selectedItem;
						}, function(){});
					});
				},
				setting: function(){
					var modalInstance = $uibModal.open({
						animation: true,
						templateUrl: 'settingModalContent.html',
						controller: function($scope, $uibModalInstance) {
							$scope.data = data;
							console.log($scope);
							$scope.save = function(){
								shopService.saveSettingInfo($scope.data).success(function(resp){
									dialogs.error(resp.title, resp.content);
									$uibModalInstance.dismiss('cancel');
								});
							}
							$scope.cancel = function() {
								$uibModalInstance.dismiss('cancel');
							};
						},
						resolve: {
							items: function(){
								return ;
							}
						}
					});
					modalInstance.result.then(function(selectedItem) {
						$scope.selected = selectedItem;
					}, function(){});
				},
				//店铺结算审核按钮
				checkWithdraw: function(){
					shopService.checkWithdraw($scope.sid).success(function(resp){
						dialogs.error(resp.title, resp.content).result.then(function(){
							location.reload(true);
						});
					})
				},
				checkWithdrawAll: function(){
					console.log($scope);
				},
			});
		},
		link: function(scope, element, attr){
			var timestamp = Date.parse(new Date())/1000;
			switch(attr.myWidget){
				case 'button':
					if(scope.vip < timestamp)
						element.find('button')[1].remove();
					else
						element.find('button')[2].remove();
					break;
				case 'status':
					if(scope.vip == 0){
						var elem = [element.find('span')[1], element.find('span')[2]];
					}else if(scope.vip < timestamp){
						var elem = [element.find('span')[0], element.find('span')[2]];
					}else{
						var limit = $filter('date')(parseInt(scope.vip) * 1000, 'yyyy年MM月dd日');
						angular.element(element.find('span')[2]).text(limit);
						var elem = [element.find('span')[0], element.find('span')[1]];
					}
					angular.element(elem).remove();
					break;
				case 'setting':
					break;
			}
		}
	}
}]);